package com.icode.web.controller.User;

import com.icode.core.dto.house.ExchangePoolDTO;
import com.icode.core.dto.house.MyHouseDTO;
import com.icode.core.dto.order.PageDTO;
import com.icode.core.dto.user.UsersInfoDTO;
import com.icode.core.model.user.UserInfo;
import com.icode.core.shared.DataToJson;
import com.icode.service.house.ExchangeService;
import com.icode.service.house.MyHouseService;
import com.icode.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Song
 * Date: 15-12-21
 * Time: 下午5:10
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/back/user_info")
public class UsersBackController {
    @Autowired
    private UserService userService;

    @Autowired
    private MyHouseService myHouseService;

    @Autowired
    private ExchangeService exchangeService;

    @RequestMapping(value = "list/{currentPage}", method = RequestMethod.GET)
    public String showAllUsersInfo(Model model, @PathVariable String currentPage) {
        String totalPage = userService.findTotalPage(UserInfo.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        List<UsersInfoDTO> usersInfoDTOs = userService.findAllUserInfo(currentPage);
        model.addAttribute("usersInfoDTOs", usersInfoDTOs);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "back/user/allUsersInfoView";
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public void showUserInfoByPhoneNum(String phoneNum, String realName, HttpServletResponse response) {
        List<UsersInfoDTO> usersInfoDTOs = userService.findUserInfoByPhoneNumAndRealName(phoneNum, realName);
        DataToJson.userInfoToJson(usersInfoDTOs, response);
    }

    @RequestMapping(value = "userStatus/{currentPage}", method = RequestMethod.POST)
    public String changeUserStatus(String phoneNum, String activate, @PathVariable String currentPage) {
        String totalPage = userService.findTotalPage(UserInfo.class).toString();
        currentPage = PageDTO.isPage(currentPage, totalPage);
        userService.changeUserStatus(phoneNum, activate);
        return "redirect:/back/user_info/list/" + currentPage;
    }

    @RequestMapping(value = "myHouse/{username}", method = RequestMethod.GET)
    public String showUserHouse(@PathVariable String username, Model model) {
        List<MyHouseDTO> myHouseDTOs = myHouseService.findMyHouseByUsername(username);
        model.addAttribute("myHouseDTOs", myHouseDTOs);
        model.addAttribute("username", username);
        return "/back/user/userInfoDetail2";
    }

    @RequestMapping(value = "searchMyExchangeHouse/{username}", method = RequestMethod.GET)
    @ResponseBody
    public List<MyHouseDTO> searchMyExchangeHouse(@PathVariable String username) {
        List<MyHouseDTO> myHouseDTOs = myHouseService.findMyExChangeHouseByUsername(username);
        return myHouseDTOs;
    }

    @RequestMapping(value = "searchMyExchangingHouse/{username}", method = RequestMethod.GET)
    @ResponseBody
    public List<MyHouseDTO> searchMyExchangingHouse(@PathVariable String username) {
        List<MyHouseDTO> myHouseDTOs = myHouseService.findMyExChangingHouseByUsername(username);
        return myHouseDTOs;
    }

}
